package com.mall.server.controller.sms;

import com.mall.model.domain.SmsCoupon;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.model.vo.SmsCouponVo;
import com.mall.server.service.sms.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/coupon")
public class SmsCouponController {

    @Autowired
    private SmsCouponService smsCouponService;

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     */
    @GetMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "10") Integer pageSize
            , SmsCoupon smsCoupon) {

        CommonPage commonPage = smsCouponService.list(pageNum, pageSize, smsCoupon);

        return CommonResult.success(commonPage);
    }

    /**
     * 获取单个优惠券的详细信息
     */
    @GetMapping("/{id}")
    public CommonResult findById(@PathVariable Integer id) {

        SmsCouponVo smsCoupon = smsCouponService.findById(id);
        return CommonResult.success(smsCoupon);
    }

    /**
     * 删除优惠券
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Integer id) {

        smsCouponService.delete(id);
        return CommonResult.success(null);
    }

    /**
     * 添加优惠券
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody SmsCouponVo smsCouponVo) {

        smsCouponService.create(smsCouponVo);

        return CommonResult.success(null);
    }

    /**
     * 修改优惠券
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@RequestBody SmsCouponVo smsCouponVo, @PathVariable Integer id) {

        smsCouponService.updateCoupon(id, smsCouponVo);

        return CommonResult.success(null);
    }

}
